package com.lazy.localfs;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * 写入本地的文件系统
 * @author luodan
 *
 */
public class WriteLocal {
    private static String  data = "100bytettttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"
            + "100bytettttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt"
            + "\r\n";
    
    
    public static void writeData(OutputStream out, byte[] buf) {
        try{
            out.write(buf);
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    
    public static void write1MData() throws Exception {
        byte[] binData = data.getBytes("UTF8");
        BufferedOutputStream out = null;
        try {
            long time = System.currentTimeMillis();
            File file = new File("data/" + Long.toHexString(time));
            if(!file.exists())
                file.createNewFile();
            out=new BufferedOutputStream(new FileOutputStream(file));
            // write 1M file
            for(int j= 0; j < 5000; j++) {
                writeData(out, binData);
            }
        } finally {
            out.close();
        }
    }
    
    public static void main(String[] args) throws Exception{
        long startTime = System.currentTimeMillis();
        for (int i= 0; i< 1000; i++) {
            write1MData();
        }
        System.out.println("took times " + (System.currentTimeMillis() - startTime));
    }
}
